package jude.java.util;

import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;

import org.apache.commons.lang.StringUtils;

public class FileUtils {
	
	public static String normalizePath(String path) {
		if (StringUtils.isEmpty(path))
			return "./";
		if (path.indexOf('\\') >= 0)
			path = path.replace('\\', '/');
		if (path.indexOf("/./") >= 0)
			path = StringUtils.replace(path, "/./", "/");
		if (path.indexOf("//") >= 0)
			path = path.replaceAll("[/]{2,}", "/");
		return path;
	}

	public static void copy(File src, File dest, FileFilter filter, boolean overwrite, boolean ignoreEmpty) throws IOException {
		if (src.isFile())
			copyFile(src, dest, overwrite);
		else if (src.isDirectory())
			copyDirectory(src, dest, filter, overwrite, ignoreEmpty);
	}

	public static void copyFile(File src, File dest, boolean overwrite) throws IOException {
		InputStream in = new FileInputStream(src);
		if (dest.exists() && !overwrite) {
			return;
		}
		OutputStream out = new FileOutputStream(dest);
		int n;
		byte[] buff = new byte[4096];
		try {
			while ((n = in.read(buff)) != -1) {
				out.write(buff, 0, n);
			}
		} finally {
			in.close();
			out.close();
		}
	}

	public static void copyDirectory(File src, File dest, boolean overwrite) throws IOException {
		copyDirectory(src, dest, null, overwrite, false);
	}
	
	public static void copyDirectory(File src, File dest, FileFilter filter, boolean overwrite, boolean ignoreEmpty) throws IOException {
		File[] children = src.listFiles(filter);
		for (File child : children) {
			File target = new File(dest.getAbsolutePath() + File.separator + child.getName());
			if(child.isFile()) {
				dest.mkdirs();
				copyFile(child, target, overwrite);
			} else if (child.isDirectory()) {
				if(!ignoreEmpty) {
					target.mkdirs();
				}
				copyDirectory(child, target, filter, overwrite, ignoreEmpty);
			}
		}
	}

	/** 彻底删除文件（夹） */
	public static void deleteFile(File file) {
		deleteFile(file, null);
	}
	
	/** 删除文件（夹） */
	public static void deleteFile(File file, FileFilter filter) {
		if(file.isFile()) {
			file.delete();
		} else if (file.isDirectory()) {
			File[] children = file.listFiles(filter);
			for (File child : children) {
				deleteFile(child, filter);
			}
			file.delete();
		}
	}
	
	/** 
	 * 获取扩展名
	 * */
	public static String getExtention(String fileName) {
		if (fileName == null)
			return null;
		int i = fileName.lastIndexOf('.');
		if (i == -1)
			return null;
		String ext = fileName.substring(i + 1);
		if (ext.indexOf('/') != -1 || ext.indexOf('\\') != -1)
			return null;
		return ext.toLowerCase();
	}
	
	public static StringBuilder readFile(File file, String encode) {
		try {
			if (encode == null)
				encode = "ISO-8859-1";
			StringBuilder r = new StringBuilder();
			Reader reader = new InputStreamReader(new FileInputStream(file), encode);
			char[] buffer = new char[4096];
			int n;
			while ((n = reader.read(buffer)) > 0) {
				r.append(buffer, 0, n);
			}
			reader.close();
			return r;
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}
	
	public static boolean writeFile(File file, String content, String encode) {
		try {
			FileOutputStream out = new FileOutputStream(file);
			out.write(content.getBytes(encode));
			out.close();
			return true;
		} catch (Exception e) {
		}
		return false;
	}
}
